﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ultimus.WFServer;
using Task.Logic;

using MyLib.Service;
using MyLib.Integration;

namespace Task
{
    public class TaskStatus : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string incident = Request.QueryString["Incident"];
            string processName = Server.UrlDecode(Request.QueryString["ProcessName"].Trim());
           
            try
            {               

                Incident.Status pstatus = new Incident.Status();
                Incident pincident = new Incident();
                pincident.LoadIncident(processName, int.Parse(incident));
                pincident.GetIncidentStatus(out pstatus);

                DataTable pGetData = this.Blank();
                //获取流程步骤状态信息
                if (pstatus.TaskStatuses == null)
                {
                    throw new Exception("frm_TaskStatus_notification");
                }

                Incident.Status.StepStatus[] pStepStatus = null;
                try
                {
                    pStepStatus = pstatus.TaskStatuses;
                }
                catch
                {
                    throw new Exception("frm_TaskStatus_notification1");
                }

                foreach (Incident.Status.StepStatus pStep in pStepStatus)
                {
                    // 5:数据库机器人 2:发起步骤 4:用户步骤 6:子流程
                    int pStepType = pStep.nStepType;
                    if (pStepType != 2 && pStepType != 4 && pStepType != 6)
                    {
                        if (pStep.strStepName.Trim().ToUpper() != "COMPLETE")
                            continue;
                    }

                    DataRow pRow = pGetData.NewRow();
                    pRow["StepName"] = pStep.strStepName;

                    string pStepUser = pStep.strStepRecipient;

                    //获取fullname
                    Ultimus.OC.OrgChart oc = new Ultimus.OC.OrgChart();
                    Ultimus.OC.User user = new Ultimus.OC.User();
                    oc.FindUser(pStepUser, "", 0, out user);
                    string pFullName = user.strUserFullName;

                    //去掉域名
                    int pIndex = pStepUser.Trim().IndexOf("/");
                    if (pIndex > 0)
                        pStepUser = pStepUser.Trim().Substring(pStepUser.Trim().IndexOf("/") + 1);

                    

                    //去掉域名
                    int pIndex1 = pFullName.Trim().IndexOf("-");
                    if (pIndex1 > 0)
                        pFullName = pFullName.Trim().Substring(pFullName.Trim().IndexOf("-") + 1);

                    pRow["StepUser"] = pFullName + "(" + pStepUser + ")";

                    if (pStep.nStepStatus.ToString() == "13")
                    {
                        pRow["StartTime"] = "**********";
                    }
                    else
                    {
                        pRow["StartTime"] = DateTime.FromOADate(pStep.dtStartTime);
                    }
                    if (pStep.nStepStatus.ToString() == "13" || pStep.nStepStatus.ToString() == "1")
                    {
                        pRow["EndTime"] = "**********";
                    }
                    else
                    {
                        pRow["EndTime"] = DateTime.FromOADate(pStep.dtEndTime);
                    }

                    if (pStep.nStepStatus.ToString() == "1")
                    {
                        pRow["Status"] = Task.Content.lang.TaskStatus_Active;
                    }
                    else if (pStep.nStepStatus.ToString() == "3")
                    {
                        pRow["Status"] = Task.Content.lang.TaskStatus_Completed;
                    }
                    else if (pStep.nStepStatus.ToString() == "4" || pStep.nStepStatus.ToString() == "7")
                    {
                        pRow["Status"] = Task.Content.lang.TaskStatus_Return;
                    }
                    else if (pStep.nStepStatus.ToString() == "13")
                    {
                        pRow["Status"] = Task.Content.lang.TaskStatus_Queue;
                    }
                    else if (pStep.nStepStatus.ToString() == "19")
                    {
                        pRow["Status"] = Task.Content.lang.TaskStatus_Failure;
                    }
                    else
                    {
                        pRow["Status"] = Task.Content.lang.TaskStatus_Unknown;
                    }

                    pGetData.Rows.Add(pRow);
                }
                //绑定数据源
                if (pGetData.Rows.Count > 0)
                {
                    this.DataBind(pGetData);
                }
                else
                {
                    //this.btn_GraphicWF.Disabled=true;
                    this.DataBindDefaultData(pGetData);
                }
            }
            catch (Exception ee)
            {
                //throw new Exception(ee.Message);
                //				msg.Value=ee.Message;
            }
        }



        /// <summary>
        /// 返回步骤拥有人的全名
        /// </summary>
        /// <param name="pTaskID"></param>
        /// <param name="pStepUser"></param>
        /// <returns></returns>
        private string ProcessStepUser(string pTaskID, string pStepUser)
        {
            string pReturn = pStepUser;

            Ultimus.WFServer.Task pTask = new Ultimus.WFServer.Task();
            bool pIsOK = pTask.InitializeFromTaskId(pTaskID);
            if (pIsOK)
            {
                if (pStepUser.Trim().IndexOf("_WF") != -1)
                {
                    pReturn = pTask.strGroupName;
                }
                else
                {
                    pReturn = pTask.strRecipientFullName;
                }
            }

            return pReturn;
        }

        /// <summary>
        /// 得到缺省的数据
        /// </summary>
        /// <returns></returns>
        private DataTable Blank()
        {
            DataTable pGetData = new DataTable();

            DataColumn pCol4 = new DataColumn("StepName");
            pGetData.Columns.Add(pCol4);

            DataColumn pCol5 = new DataColumn("StepUser");
            pGetData.Columns.Add(pCol5);

            DataColumn pCol6 = new DataColumn("StartTime");
            pGetData.Columns.Add(pCol6);

            DataColumn pCol7 = new DataColumn("EndTime");
            pGetData.Columns.Add(pCol7);

            DataColumn pCol8 = new DataColumn("Status");
            pGetData.Columns.Add(pCol8);

            return pGetData;
        }


        //目的  设置网格行数
        //参数  myDataTable－数据表；intPageCount－每页行数；intCol－加空行的列号
        public static void SetTableRows(ref DataTable myDataTable, int intPageCount)
        {
            int intTemp = myDataTable.Rows.Count % intPageCount;
            if ((myDataTable.Rows.Count == 0) || (intTemp != 0))
            {
                for (int i = 0; i < (intPageCount - intTemp); i++)
                {
                    DataRow myDataRow = myDataTable.NewRow();
                    myDataTable.Rows.Add(myDataRow);
                }
            }
        }

        /// <summary>
        /// 绑定缺省的空数据
        /// </summary>
        /// <param name="pBindData"></param>
        private void DataBindDefaultData(DataTable pDataTable)
        {
            
            this.rptTaskList.DataSource = pDataTable.DefaultView;
            this.rptTaskList.DataBind();
        }

        /// <summary>
        /// 绑定数据源
        /// </summary>
        /// <param name="pDataTable"></param>
        public void DataBind(DataTable pDataTable)
        {
            this.rptTaskList.DataSource = pDataTable.DefaultView;
            this.rptTaskList.DataBind();

        }



        protected global::System.Web.UI.WebControls.Repeater rptTaskList;

    }
}